<template>
	<div class="login-wrap">
		<div class="ms-title">环保管家</div>
		<div class="ms-login">
			<div class="login-top" @click="isLogin=false" v-show="isLogin"></div>
			<div class="register-top" @click="isLogin=true" v-show="!isLogin"></div>
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm" v-show="isLogin">
				<p class="login-txt">登陆</p>
				<el-form-item prop="username">
					<el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
				</div>
				<p style="font-size:12px;line-height:30px;color:#999;">Tips : 点击二维码进入注册</p>
			</el-form>
			<el-form class="register" v-show="!isLogin">
				<p class="login-txt">注册请扫描</p>
				<canvas id="registerQrcode"></canvas>
			</el-form>
		</div>
	</div>
</template>

<script>
	export default {
		mounted: function() {
		    this.$nextTick(function() {
				var QRCode = require('qrcode');
				var canvas = document.getElementById('registerQrcode');
				var opt = {
					scale:5
				};
				QRCode.toCanvas(canvas, this.request_url+'wx/wxPubOauth?redirect=register',opt, function (error) {

			  	});
		    });
		},
		data: function() {
			return {
				login_img: "../../assets/img/login-top.png",
				isLogin: true,
				ruleForm: {
					username: 'admin',
					password: 'admin123'
				},
				rules: {
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' },
						{ min: 6, message: '密码不能小于6位数', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			submitForm(formName) {
				const self = this;
				self.$refs[formName].validate((valid) => {
					if(valid) {
						if (self.ruleForm.username == 'admin' && self.ruleForm.password == 'admin123') {
							localStorage.setItem('token', 1);
							self.$router.push('/');
						}else{
							alert('密码错误');
						}
					} else {
						console.log('error submit!!');
					}
				});
			},
		}
	}	 
</script>

<style lang="less">
	.login-wrap {
		position: relative;
		width: 100%;
		height: 100%;
	}
	
	.ms-title {
		position: absolute;
		top: 50%;
		width: 100%;
		margin-top: -230px;
		text-align: center;
		font-size: 30px;
		color: #fff;
	}
	
	.ms-login {
		position: absolute;
		left: 50%;
		top: 50%;
		width: 300px;
		height: 240px;
		margin: -150px 0 0 -190px;
		padding: 40px;
		padding-top: 20px;
		border-radius: 5px;
		background: #fff;
		.login-top {
			position: absolute;
			right: 0;
			top: 0;
			width: 50px;
			height: 50px;
			background: url(../../assets/img/login-top.png) right top no-repeat #fff;
			border-radius: 10px;
		}
		.register-top {
			position: absolute;
			right: 0;
			top: 0;
			width: 50px;
			height: 50px;
			background: url(../../assets/img/login.png) right top no-repeat #fff;
			border-radius: 10px;
		}
		.login-txt {
			margin-bottom: 20px;
			font-size: 16px;
			color: #3c3c3c;
		}
		.login-btn {
			text-align: center;
		}
		.login-btn button {
			width: 100%;
			height: 36px;
		}
		.register {
			text-align: center;
			p {
				text-align: left;
			}
			img {
				height: 200px;
			}
		}
		.el-form-item {
			margin-bottom: 30px;
		}
	}
</style>